//
//  UIDevice+MZ.m
//  meizhuang
//
//  Created by Ricky on 16/8/11.
//  Copyright © 2016年 netease. All rights reserved.
//

#import <sys/sysctl.h>
#import <sys/mount.h>
#import <mach/mach.h>

#import "UIDevice+WF.h"

#define UNIFIED_DEVICD_ID_SERVICE @"com.netease.mei.device.uuid"

@implementation UIDevice (WF)

- (NSString *)mz_devicePlatform {
    static NSString *_platform = nil;
    if (!_platform) {
        size_t size;
        char machine[64] = {0};
        sysctlbyname("hw.machine", machine, &size, NULL, 0);
        if (strlen(machine)) {
            _platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
        }
    }
    return _platform;
}

- (NSString *)mz_deviceModel
{
    static NSString * _model = nil;
    if (!_model) {
        NSString *platform = [self mz_devicePlatform];
        if (platform) {
            _model = [self platformType:platform];
        } else {
            _model = self.model;
        }
    }
    return _model;
}

- (NSString *) platformType:(NSString *)platform
{
    if ([platform isEqualToString:@"iPhone1,1"])      return @"iPhone (1st generation)";
    if ([platform isEqualToString:@"iPhone1,2"])      return @"iPhone 3G";
    if ([platform isEqualToString:@"iPhone2,1"])      return @"iPhone 3GS";
    if ([platform isEqualToString:@"iPhone3,1"])      return @"iPhone 4 (GSM)";
    if ([platform isEqualToString:@"iPhone3,2"])      return @"iPhone 4 (GSM Rev A)";
    if ([platform isEqualToString:@"iPhone3,3"])      return @"iPhone 4 (CDMA)";
    if ([platform isEqualToString:@"iPhone4,1"])      return @"iPhone 4S";
    if ([platform isEqualToString:@"iPhone5,1"])      return @"iPhone 5 (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone5,2"])      return @"iPhone 5 (CDMA/LTE)";
    if ([platform isEqualToString:@"iPhone5,3"])      return @"iPhone 5c (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone5,4"])      return @"iPhone 5c (CDMA/LTE)";
    if ([platform isEqualToString:@"iPhone6,1"])      return @"iPhone 5s (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone6,2"])      return @"iPhone 5s (CDMA/LTE)";
    if ([platform isEqualToString:@"iPhone7,2"])      return @"iPhone 6";
    if ([platform isEqualToString:@"iPhone7,1"])      return @"iPhone 6 Plus";
    if ([platform isEqualToString:@"iPhone8,1"])      return @"iPhone 6s";
    if ([platform isEqualToString:@"iPhone8,2"])      return @"iPhone 6s Plus";
    if ([platform isEqualToString:@"iPhone8,4"])      return @"iPhone SE";
    if ([platform isEqualToString:@"iPhone9,1"])      return @"iPhone 7 (CDMA+GSM/LTE)";
    if ([platform isEqualToString:@"iPhone9,3"])      return @"iPhone 7 (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone9,2"])      return @"iPhone 7 Plus (CDMA+GSM/LTE)";
    if ([platform isEqualToString:@"iPhone9,4"])      return @"iPhone 7 Plus (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,1"])      return @"iPhone 8 (CDMA+GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,4"])      return @"iPhone 8 (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,2"])      return @"iPhone 8 Plus (CDMA+GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,5"])      return @"iPhone 8 Plus (GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,3"])      return @"iPhone X (CDMA+GSM/LTE)";
    if ([platform isEqualToString:@"iPhone10,6"])      return @"iPhone X (GSM/LTE)";
    
    if ([platform isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([platform isEqualToString:@"iPad2,1"])      return @"iPad 2 (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([platform isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([platform isEqualToString:@"iPad2,4"])      return @"iPad 2 (Wi‑Fi, A5R)";
    if ([platform isEqualToString:@"iPad3,1"])      return @"iPad (3rd generation, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad3,2"])      return @"iPad (3rd generation, GSM/LTE)";
    if ([platform isEqualToString:@"iPad3,3"])      return @"iPad (3rd generation, CDMA/LTE)";
    if ([platform isEqualToString:@"iPad3,4"])      return @"iPad (4th generation, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad3,6"])      return @"iPad (4th generation, CDMA/LTE)";
    if ([platform isEqualToString:@"iPad3,5"])      return @"iPad (4th generation, GSM/LTE)";
    if ([platform isEqualToString:@"iPad4,1"])      return @"iPad Air (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad4,2"])      return @"iPad Air (LTE)";
    if ([platform isEqualToString:@"iPad4,3"])      return @"iPad Air (China)";
    if ([platform isEqualToString:@"iPad5,3"])      return @"iPad Air 2 (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad5,4"])      return @"iPad Air 2 (LTE)";
    if ([platform isEqualToString:@"iPad6,11"])      return @"iPad (5th generation, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad6,12"])      return @"iPad (5th generation, LTE)";
    if ([platform isEqualToString:@"iPad6,7"])      return @"iPad Pro (12.9‑inch, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad6,8"])      return @"iPad Pro (12.9‑inch, LTE)";
    if ([platform isEqualToString:@"iPad6,3"])      return @"iPad Pro (9.7‑inch, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad6,4"])      return @"iPad Pro (9.7‑inch, LTE)";
    if ([platform isEqualToString:@"iPad7,1"])      return @"iPad Pro (12.9‑inch, 2nd generation, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad7,2"])      return @"iPad Pro (12.9‑inch, 2nd generation, LTE)";
    if ([platform isEqualToString:@"iPad7,3"])      return @"iPad Pro (10.5‑inch, Wi‑Fi)";
    if ([platform isEqualToString:@"iPad7,4"])      return @"iPad Pro (10.5‑inch, LTE)";
    if ([platform isEqualToString:@"iPad2,5"])      return @"iPad mini (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad2,6"])      return @"iPad mini (GSM/LTE)";
    if ([platform isEqualToString:@"iPad2,7"])      return @"iPad mini (CDMA/LTE)";
    if ([platform isEqualToString:@"iPad4,4"])      return @"iPad mini with Retina display (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad4,5"])      return @"iPad mini with Retina display (LTE)";
    if ([platform isEqualToString:@"iPad4,6"])      return @"iPad mini with Retina display (China)";
    if ([platform isEqualToString:@"iPad4,7"])      return @"iPad mini 3 (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad4,8"])      return @"iPad mini 3 (LTE)";
    if ([platform isEqualToString:@"iPad4,9"])      return @"iPad mini 3 (China)";
    if ([platform isEqualToString:@"iPad5,1"])      return @"iPad mini 4 (Wi‑Fi)";
    if ([platform isEqualToString:@"iPad5,2"])      return @"iPad mini 4 (LTE)";
    
    if ([platform isEqualToString:@"iPod1,1"])      return @"iPod touch (1st generation)";
    if ([platform isEqualToString:@"iPod2,1"])      return @"iPod touch (2nd generation)";
    if ([platform isEqualToString:@"iPod3,1"])      return @"iPod touch (3rd generation)";
    if ([platform isEqualToString:@"iPod4,1"])      return @"iPod touch (4th generation)";
    if ([platform isEqualToString:@"iPod5,1"])      return @"iPod touch (5th generation)";
    if ([platform isEqualToString:@"iPod7,1"])      return @"iPod touch (6th generation)";
    
    if ([platform isEqualToString:@"AppleTV2,1"])   return @"Apple TV 2G";
    if ([platform isEqualToString:@"AppleTV3,1"])   return @"Apple TV 3";
    if ([platform isEqualToString:@"AppleTV3,2"])   return @"Apple TV 3 (2013)";
    
    if ([platform isEqualToString:@"i386"])         return @"Simulator";
    if ([platform isEqualToString:@"x86_64"])       return @"Simulator";
    
    return platform;
}

- (BOOL)isIPhoneX {
#if TARGET_OS_SIMULATOR
    CGFloat scale = [UIScreen mainScreen].nativeScale;
    if (CGSizeEqualToSize([UIScreen mainScreen].nativeBounds.size, CGSizeMake(375 * scale, 812 * scale))) {
        return YES;
    }
    return NO;
#else
    return (
            [self.mz_devicePlatform isEqualToString:@"iPhone10,3"] ||
            [self.mz_devicePlatform isEqualToString:@"iPhone10,6"]
            );
#endif
}

- (double)mz_totalDiskspaceBytes {
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0)
    {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_blocks);
    }
    return freeSpace;
    
}

- (double)mz_availableDiskspaceBytes {
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0)
    {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
    }
    return freeSpace;
}

- (double)mz_totalMemoryBytes {
    return [NSProcessInfo processInfo].physicalMemory;
}

- (double)mz_availableMemoryBytes {
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    if (kernReturn != KERN_SUCCESS)
    {
        return NSNotFound;
    }
    return ((vm_page_size * vmStats.free_count + vm_page_size * vmStats.inactive_count));
}

@end
